function hbw() {
  $('main .box').slideToggle();
  $('main .box .sort').css('display', 'flex')
  $('main .box .norm').css('display', 'none')
  $('main .box .default').css('display', 'none')
}

function quanbu() {
  $('main .box').slideToggle();
  $('main .box .norm').css('display', 'block')
  $('main .box .sort').css('display', 'none')
  $('main .box .default').css('display', 'none')
}

function moren() {
  $('main .box').slideToggle();
  $('main .box .default').css('display', 'block')
  $('main .box .sort').css('display', 'none')
  $('main .box .norm').css('display', 'none')
}

$('main .con_list li').click(function () {
  window.location.href = "/view/index/spxq_gm.html";
});
var vm = new Vue({
  el: "#app",
  data: {
    leixing_list: [],//类型列表
    index_1: 0,//第一级的index
    index_2: 0,//第二级的index
    index_3: 0,//第三级的index
    index_4: 0,//第四级的index
    now_tabs:0,
    all_fenlei_item:{
      name:'全部分类',
      istag:true
    },
    fenlei_type:'all',
    fenlei_name:'全部分类',
    all_size_list:[],//全部规格列表
    all_item:{
      name:'全部规格',
      id:'all',
      istag:true
    },
    size_id:'all',//规格id,默认为全部
    size_name:'全部规格',
    order_list:[
      {
        name:'默认排序',
        order:'默认',
        istag:true
      },
      {
        name:'价格从低到高',
        order:'amount asc',
        istag: false
      },
      {
        name:'价格从高到低',
        order:'amount desc',
        istag: false
      }
    ],//排序列表
    order_type:'默认',//排序模式
    order_name:'默认排序',//排序标题
    limit:10,
    page:1,
    render_list:[],//下面的渲染的列表
    finished:false,
    loading:false,
    lock:false,
    value_pwd:'',//输入密码
    now_see_pwd:'',//记录当前项的see_pwd
    now_item_id:'',
    show_input_pwd:false,//显示输入密码的遮罩层
  },
  computed: {
    // 四个数组层层依赖
    list_1: function () {
      return this.leixing_list;
    },
    list_2: function () {
      if (this.leixing_list.length > 0) {
        return this.list_1[this.index_1].child
      } else {
        return []
      }
    },
    list_3: function () {
      if (this.leixing_list.length > 0) {
        return this.list_2[this.index_2].child
      } else {
        return []
      }
    },
    list_4: function () {
      if (this.leixing_list.length > 0) {
        return this.list_3[this.index_3].child
      } else {
        return []
      }
    }
  },
  methods: {
    init: function () {
      this.get_leixing_list();
      this.get_all_size_list();
      this.get_render_list();
      console.log('cccc')//
    },
    // 搜索跳转
    search_link:function(){
      window.location.href = '/view/index/wycs2.html';
    },
    get_leixing_list: function (suc) {
      var that = this;
      $.post(app.webUrl + '/view/api_activity/typelist', {}, cb).fail(function (err) {
        console.log(err)
      });
      function cb(ret) {
        if (ret.code == '1') {
          that.leixing_list = ret.data;
          console.log(that.leixing_list);
          // 获取渲染列表
        }
      }
    },
    //获取全部规格列表,三级列表...
    get_all_size_list:function(){
      var that = this;
      $.post(app.webUrl + '/view/api_activity/typelist', {
        type:'1',
        floor:'2'
      }, function(ret){
        if(ret.code == 1){
          that.all_size_list = ret.data;
        }
      }).fail(function (err) {
        console.log(err)
      });
    },
    showTabs:function(i){
      if(this.now_tabs == i){
        this.hideBox();
      }else{
        this.now_tabs = i;
      }
    },
    // 关闭遮罩层,在关闭遮罩层时刷新列表
    hideBox:function(){
      this.now_tabs = 0;
      // 刷新列表
      this.page = 1;
      this.lock = false;
      this.loading = false;
      this.finished = false;
      this.render_list = [];
      app.toastLoading('加载中')
      this.get_render_list(cb);
      function cb(ret){
        app.toastClear();
      }
    },
    // 选择一级分类
    choose_list_1:function(index,item){
      var that = this;
      if(index == 'all'){
        this.list_1.forEach(function(item){
          item.istag = false;
        });
        this.all_fenlei_item.istag = true;
        this.fenlei_name = this.all_fenlei_item.name;
        this.fenlei_type = 'all';
        return;
      }
      this.all_fenlei_item.istag = false;
      this.index_1 = index;
      this.list_1.forEach(function(item){
        item.istag = false;
      });
      this.list_1[index].istag = true;
      this.fenlei_type = 'not_all';
      // 默认选择二级分类的第一个,但是不隐藏
      this.$nextTick(function(){
        // $('.sort-right li').eq(0).trigger('click');
        that.choose_list_2(0,'no_hide');//自动选择第一个
      })
    },
    // 选择二级分类
    choose_list_2:function(index,_val){
      this.index_2 = index;
      this.list_2.forEach(function(item){
        item.istag = false;
      });
      this.list_2[index].istag = true;
      this.fenlei_name = this.list_2[index].name;
      this.$forceUpdate();
      // 选择玩隐藏页面并刷新列表
      if(_val != 'no_hide'){
        this.hideBox();
      }
    },
    // 选择规格,三级列表
    choose_size:function(item){
      this.all_item.istag = false;
      this.list_3.forEach(function(item){
        item.istag = false;
      });
      item.istag = true;
      this.size_id = item.id;
      this.size_name = item.name;
      this.$forceUpdate();
      this.hideBox();
    },
    // 选择排序
    choose_order:function(item){
      this.order_list.forEach(function(item){
        item.istag = false;
      });
      item.istag = true;
      this.order_type = item.order;
      this.order_name = item.name;
      this.hideBox();
    },
    //获取列表
    get_render_list:function(cb){
      var that = this;
      var value = {
        limit:that.limit,
        page:that.page,
        status:'1',
      };
      if(that.fenlei_name == '全部分类' && that.size_id == 'all'){
        // 当选项为全部分类并且全部规格的时候,获取全部的列表
        console.log('全部');
      }else{
        if(that.size_id == 'all'){
          // 传二级id
          value.bid = that.list_2[that.index_2].id;
        }else{
          // 传三级id
          value.cid = that.size_id;
        }
      }
      if(that.order_type != '默认'){
        value.order = that.order_type;
      };
      console.log(value);
      $.post(app.webUrl+'/view/api_card/lists',value,function(ret){
        // console.log(ret);
        if(ret.code == '1'){
          for(var i=0; i<ret.data.result.length; i++){
            that.render_list.push(ret.data.result[i]);
            that.page++;
          };
        };
        if(typeof cb =='function'){
          cb(ret);
        };
        that.open_lock();
      }).fail(function(err){
        console.log(err);
        that.open_lock();
      })
    },
    // 分页加载
    onLoad:function(){
      var that = this;
      if(!this.lock){
        console.log('false');
        return;
      }
      this.lock = false;
      this.get_render_list(cb);
      function cb(ret){
        console.log(88)
        console.log(ret);
        if(ret.code == '0' && !ret.data.result){
          that.finished = true;
        }
      }
    },
    // 打开lock
    open_lock:function(){
      var that = this;
      setTimeout(function(){
        that.lock = true;
        that.loading = false;
      },500)
    },
    // 点击列表的每项
    click_item:function(item){
      if(item.ensure=='1'){
        this.dailog_input(item)
      }else{
        // 跳转
        this.link_to_detailPage(item.id)
      }
    },
    // 弹出密码输入的遮罩层
    dailog_input:function(item){
      this.show_input_pwd = true;
      this.now_see_pwd = item.see_pwd;
      this.now_item_id = item.id;
    },
    //关闭
    close_input:function(){
      this.show_input_pwd = false;
    },
    //确认密码
    pwd_confirm:function(){
      if(this.now_see_pwd == app.doublemd5(this.value_pwd)){
        // alert('密码正确');
        vant.Toast('查看密码正确')
        this.link_to_detailPage(this.now_item_id);
      }else{
        vant.Toast('查看密码错误')
      };
        var vv = app.doublemd5(this.value_pwd)
        
        console.log(vv)
      this.value_pwd = '';
      this.close_input();
    },
    // 跳转到商品详情页面
    link_to_detailPage:function(id){
      window.location.href='/view/index/spxq_cs.html?&activityId='+id;
    }
  },
  mounted: function () {
    this.init()
  },
})
